'use strict';

/* Controllers */
app.controller('houseOwnerContractListCtl', function($scope,$compile,ngDialog,uiLoad,user,rest,tipService) {
    // var rentTypeObj = {
    //     0: "年付",
    //     1: "季付",
    //     2: "月付"
    // };
    // var applyTypeObj = {
    //     0: "支付包",
    //     1: "微信",
    //     2: "现金"
    // };
    // $scope.checkType = {};
    // $scope.houseType = {}
    $scope.status = {};
    $scope.checkTypeArray = [{ "name": "全部", "value": "" }, { "name": "待确认", "value": "1" }, { "name": "己确认", "value": "2" }, { "name": "己失效", "value": "3" }, { "name": "解约", "value": "4" }, { "name": "退租", "value": "5" }];
    if($scope.queryParams){

    }
    $scope.queryParams = {
        // 'decorateStatus': $scope.houseType.selected || 0,
        // 'startTime': $scope.startTime,
        // 'endTIme': $scope.endTIme,
        // 'nameCode': $scope.nameCode
        // landlordId: user.get('landlordId')
    }
    $scope.queryUser = function() {
        // for(var i in $scope.checkTypeArray){
        //     if($scope.queryParams.checkStatus == $scope.checkTypeArray[i].name){
        //         $scope.queryParams.checkStatus = $scope.checkTypeArray[i].value;
        //             debugger;
        //         break;
        //     }
        // }
        // $scope.params = angular.extend({ LandlordId: user.get('landlordId') }, $scope.queryParams);
        // console.log($scope.params.landlordId);
        if($scope.queryParams.status && $scope.queryParams.status.selected){
            var statusCache = $scope.queryParams.status.selected;
            if($scope.queryParams.status.selected){
                $scope.queryParams.status = $scope.queryParams.status.selected.value
            }
            $scope.dataTable.fnClearTable();
            $scope.queryParams.status = {};
            $scope.queryParams.status.selected = {};
            $scope.queryParams.status.selected = statusCache;
        }else{
            $scope.dataTable.fnClearTable();
        }
    };

    $scope.$watch('_roomCode', function(newValue, oldValue) {
         $scope.queryParams.roomCode = newValue;
    });
    $scope.$watch('_tenantsName', function(newValue, oldValue) {
         $scope.queryParams.tenantsName = newValue;
    });
    
    $scope.tableContractDatas = {
        "sAjaxSource": "4020",
        "aoColumns": [{
            "mDataProp": "status",
            "sTitle": "状态",
            "sDefaultContent": "",
            "sClass": "center failure"
        }, {
            "mDataProp": "areaName",
            "sTitle": "名称",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "houseAddress",
            "sTitle": "地址",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "roomCode",
            "sTitle": "房间号",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "houseType",
            "sTitle": "房型",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "tenantsName",
            "sTitle": "租客姓名",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "tenantsPhone",
            "sTitle": "租客电话",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "payType",
            "sTitle": "支付方式",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "rent",
            "sTitle": "实际租金",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "leaseTime",
            "sTitle": "租期",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "payDay",
            "sTitle": "交租日期",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "operate",
            "sTitle": "操作",
            "sDefaultContent": "",
            "sClass": "center handle-td operation"
        }],
        // "aoColumnDefs": [ //设置列的属性，此处设置第一列不排序
        //         {
        //             "bSortable": false,
        //             "aTargets": [0]
        //         }, {
        //             "class": "tn",
        //             "targets": [0]
        //         }, {
        //             "targets": -1,
        //             "class": "but_xq",
        //             "data": null,
        //             "bSortable": false,
        //             "defaultContent": "<p>&nbsp;&nbsp;<span id=\"edit\" >修改1</span>&nbsp;&nbsp;<span id=\"del\">删除</span></p>",
        //             "mRender" : function(data, type, full){
        //                 // for(var i in checkTypeArray){
        //                 //     if(checkTypeArray[i].value == full.checkStatus){
        //                 //         full.checkStatus = $scope.checkTypeArray[i].name;
        //                 //         break;
        //                 //     }
        //                 // }
        //                 // for(var i in applyTypeObj){
        //                 //     if(i == full.payType){
        //                 //         full.payType = applyTypeObj[i];
        //                 //         break;
        //                 //     }
        //                 // }
        //             }
        //         }
        // ],
        "fnRowCallback": function(nRow, aData, iDisplayIndex) {
            var btnStr = "";
            var failureStr = "";
            var refundStr = "";
            var rentStr = "";
            $scope.statusValue = $scope.queryParams.status;
            if ("number" == typeof $scope.statusValue) {
                $scope.checkTypeArray.map(function(ele, index) {
                    if ($scope.statusValue == ele.value) {
                        $scope.status.selected = ele;
                    }
                })
            }

            if ("己失效" == aData.status) {
                btnStr += "<a><img src='img/operation1.png' width='20px' height='20px' /></a>";
            }else if("己确认" == aData.status) {
                if (aData.isRefused == 0) {
                        if (aData.isOverdue == 1) {
                            btnStr += "<a class='operation-btn'>\
                                <img src='img/operation.png' width='20px' height='20px' />\
                                <div class='operation-none' ng-mouseleave='operation_none_mouseleave($event)'>\
                                    <dl class='operation oper-triangle'>\
                                        <dd><a href='javascript:'ng-click='qrcode(\"" + aData.id + "\",\"" + aData.openId +"\",\"" + aData.areaName + aData.houseAddress + aData.roomCode + "\")'>二维码</a></dd>\
                                        <dd><a href='javascript:'ng-click='viewContractDialog(\"" + aData.id + "\")'>查看合同</a></dd>\
                                        <dd><a href='javascript:'ng-click='viewContractPDFDialog(\"" + aData.id + "\")'>查看PDF</a></dd>\
                                        <dd><a href='javascript:'ng-click='compelOffLease(\"" + aData.id + "\")'>强制退租</a></dd>\
                                        <span class='z'>◆</span><span class='y'>◆</span>\
                                    </dl>\
                                </div>\
                            </a>";
                        }else if(aData.payMonthlyStatus == "OrderRepayment" || aData.payMonthlyStatus == null){
                            btnStr += "<a class='operation-btn'>\
                                <img src='img/operation.png' width='20px' height='20px' />\
                                <div class='operation-none' ng-mouseleave='operation_none_mouseleave($event)'>\
                                    <dl class='operation oper-triangle'>\
                                        <dd><a href='javascript:'ng-click='qrcode(\"" + aData.id + "\",\"" + aData.openId +"\",\"" + aData.areaName + aData.houseAddress + aData.roomCode + "\")'>二维码</a></dd>\
                                        <dd><a href='javascript:'ng-click='viewContractDialog(\"" + aData.id + "\")'>查看合同</a></dd>\
                                        <dd><a href='javascript:'ng-click='viewContractPDFDialog(\"" + aData.id + "\")'>查看PDF</a></dd>\
                                        <dd><a href='javascript:'ng-click='applyOffLease(\"" + aData.id + "\", \"" + aData.openId + "\")'>申请退租</a></dd>\
                                        <span class='z'>◆</span><span class='y'>◆</span>\
                                    </dl>\
                                </div>\
                            </a>";
                        }else if(aData.payMonthlyStatus == "OrderPreOutMemberApply"){
                            if (aData.contractExit.isLandlordSend == 0) {
                                btnStr += "<a class='operation-btn'>\
                                    <img src='img/operation.png' width='20px' height='20px' />\
                                    <div class='operation-none' ng-mouseleave='operation_none_mouseleave($event)'>\
                                        <dl class='operation oper-triangle'>\
                                            <dd><a href='javascript:'ng-click='qrcode(\"" + aData.id + "\",\"" + aData.openId +"\",\"" + aData.areaName + aData.houseAddress + aData.roomCode + "\")'>二维码</a></dd>\
                                            <dd><a href='javascript:'ng-click='viewContractDialog(\"" + aData.id + "\")'>查看合同</a></dd>\
                                            <dd><a href='javascript:'ng-click='viewContractPDFDialog(\"" + aData.id + "\")'>查看PDF</a></dd>\
                                            <dd><a href='javascript:'ng-click='agreeOffLease(\"" + aData.id + "\",\"" + aData.contractExit.exitDate + "\")'>同意退租</a></dd>\
                                            <span class='z'>◆</span><span class='y'>◆</span>\
                                        </dl>\
                                    </div>\
                                </a>";
                            }else{
                                btnStr += "<a class='operation-btn'>\
                                    <img src='img/operation.png' width='20px' height='20px' />\
                                    <div class='operation-none' ng-mouseleave='operation_none_mouseleave($event)'>\
                                        <dl class='operation oper-triangle'>\
                                            <dd><a href='javascript:'ng-click='qrcode(\"" + aData.id + "\",\"" + aData.openId +"\",\"" + aData.areaName + aData.houseAddress + aData.roomCode + "\")'>二维码</a></dd>\
                                            <dd><a href='javascript:'ng-click='viewContractDialog(\"" + aData.id + "\")'>查看合同</a></dd>\
                                            <dd><a href='javascript:'ng-click='viewContractPDFDialog(\"" + aData.id + "\")'>查看PDF</a></dd>\
                                            <span class='z'>◆</span><span class='y'>◆</span>\
                                        </dl>\
                                    </div>\
                                </a>";
                            }
                        }else if(aData.payMonthlyStatus == "OrderPreOutApply"){
                            btnStr += "<a class='operation-btn'>\
                                <img src='img/operation.png' width='20px' height='20px' />\
                                <div class='operation-none' ng-mouseleave='operation_none_mouseleave($event)'>\
                                    <dl class='operation oper-triangle'>\
                                        <dd><a href='javascript:'ng-click='qrcode(\"" + aData.id + "\",\"" + aData.openId +"\",\"" + aData.areaName + aData.houseAddress + aData.roomCode + "\")'>二维码</a></dd>\
                                        <dd><a href='javascript:'ng-click='viewContractDialog(\"" + aData.id + "\")'>查看合同</a></dd>\
                                        <dd><a href='javascript:'ng-click='viewContractPDFDialog(\"" + aData.id + "\")'>查看PDF</a></dd>\
                                        <span class='z'>◆</span><span class='y'>◆</span>\
                                    </dl>\
                                </div>\
                            </a>";
                        }
                }

                if (aData.isRefused == 1) {
                    btnStr += "<a class='operation-btn'>\
                        <img src='img/operation.png' width='20px' height='20px' />\
                        <div class='operation-none' ng-mouseleave='operation_none_mouseleave($event)'>\
                            <dl class='operation oper-triangle'>\
                                <dd><a href='javascript:'ng-click='qrcode(\"" + aData.id + "\",\"" + aData.openId +"\",\"" + aData.areaName + aData.houseAddress + aData.roomCode + "\")'>二维码</a></dd>\
                                <dd><a href='javascript:'ng-click='viewContractDialog(\"" + aData.id + "\")'>查看合同</a></dd>\
                                <dd><a href='javascript:'ng-click='viewContractPDFDialog(\"" + aData.id + "\")'>查看PDF</a></dd>\
                                <dd><a href='javascript:'ng-click='resubmitPay(\"" + aData.id + "\", \"" + aData.refusedReason + "\")'>重新提交月付</a></dd>\
                                <span class='z'>◆</span><span class='y'>◆</span>\
                            </dl>\
                        </div>\
                    </a>";
                }

            }else{
                if (aData.isRefused == 0) {
                    btnStr += "<a class='operation-btn'>\
                        <img src='img/operation.png' width='20px' height='20px' />\
                        <div class='operation-none' ng-mouseleave='operation_none_mouseleave($event)'>\
                            <dl class='operation oper-triangle'>\
                                <dd><a href='javascript:'ng-click='qrcode(\"" + aData.id + "\",\"" + aData.openId +"\",\"" + aData.areaName + aData.houseAddress + aData.roomCode + "\")'>二维码</a></dd>\
                                <dd><a href='javascript:'ng-click='viewContractDialog(\"" + aData.id + "\")'>查看合同</a></dd>\
                                <dd><a href='javascript:'ng-click='viewContractPDFDialog(\"" + aData.id + "\")'>查看PDF</a></dd>\
                                <span class='z'>◆</span><span class='y'>◆</span>\
                            </dl>\
                        </div>\
                    </a>";
                }
                if (aData.isRefused == 1) {
                    btnStr += "<a class='operation-btn'>\
                        <img src='img/operation.png' width='20px' height='20px' />\
                        <div class='operation-none' ng-mouseleave='operation_none_mouseleave($event)'>\
                            <dl class='operation oper-triangle'>\
                                <dd><a href='javascript:'ng-click='qrcode(\"" + aData.id + "\",\"" + aData.openId +"\",\"" + aData.areaName + aData.houseAddress + aData.roomCode + "\")'>二维码</a></dd>\
                                <dd><a href='javascript:'ng-click='viewContractDialog(\"" + aData.id + "\")'>查看合同</a></dd>\
                                <dd><a href='javascript:'ng-click='viewContractPDFDialog(\"" + aData.id + "\")'>查看PDF</a></dd>\
                                <dd><a href='javascript:'ng-click='resubmitPay(\"" + aData.id + "\", \"" + aData.refusedReason + "\")'>重新提交月付</a></dd>\
                                <span class='z'>◆</span><span class='y'>◆</span>\
                            </dl>\
                        </div>\
                    </a>";
                }
            }
            
            if ("己失效" == aData.status){
                failureStr = "<div><div class='failure-bj1'>己失效</div><div class='failure-bj'></div></div>"
            }
            if("待确认" == aData.status){
                failureStr = "<div><div class='rent-bj1'>待确认</div><div class='rent-bj'></div></div>"
            }
            if("己确认" == aData.status){
                
                if (aData.payMonthlyStatus == "OrderPreOutMemberApply") {
                    failureStr = "<div><div class='rent-bj2'>退租确认中</div><div class='rent-bj_3'></div></div>"
                }else if(aData.payMonthlyStatus == "OrderPreOutApply"){
                    failureStr = "<div><div class='rent-bj1'>即将退租</div><div class='rent-bj_2'></div></div>"
                }else if(aData.isRefused == 1){
                    failureStr = "<div><div class='applySecond-bj1'>待重提</div><div class='applySecond-bj'></div></div>"
                }else{
                    failureStr = "<div><div class='confirmed-bj1'>已确认</div><div class='confirmed-bj'></div></div>"
                }
                
            }
            if("退租" == aData.status){
                failureStr = "<div><div class='refund-bj1'>已退租</div><div class='refund-bj'></div></div>"
            }
            if("解约" == aData.status){
                failureStr = "<div><div class='termination-bj1'>已解约</div><div class='termination-bj'></div></div>"
            }

            $(nRow).find(".handle-td").append($compile(btnStr)($scope));
            $(nRow).find(".failure").empty().append($compile(failureStr)($scope));
            $(nRow).find(".operation-btn").on('click', function(){
                var sopt = $(this).parent().find(".operation-none");
                if (sopt.css('display') == 'none') {
                    $(".operation-none").hide();
                    sopt.show();
                } else {
                    sopt.hide();
                }
            });
        },
        "fnServerParams": function(aoData) {
            if($scope.status && $scope.status.selected){
                $scope.queryParams.status = $scope.status.selected.value
            }
            aoData.push({
                'name': 'queryParams',
                'value': $scope.queryParams
            });
            return aoData;
        }
    };
    $scope.dataTable = undefined;
    
    $scope.advancePlayList = function (contractId, openId, title) {
        ngDialog.openConfirm({
          template: 'js/module/advancePlayList/advancePlayList.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/module/advancePlayList/advancePlayList.js'])
            }
          },
          data:{
            contractId: contractId
          },
          scope: $scope,
          width: 1000,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });        
    }

    $scope.checkContract = function(){
        
    }

    $scope.resubmitPay = function(contractId,refusedReason){
        ngDialog.openConfirm({
          template: 'js/module/fund/confirmBox.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/module/fund/confirmBox.js'])
            }
          },
          data:{       
            contractId: contractId,     
            refusedReason: refusedReason
          },
          scope: $scope,
          width: 800,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });
    }

    $scope.viewContractPDFDialog = function (contractId){
        // alert("正在打开新窗口显示PDF合同");
        tipService.show("body", {
            message: "正在打开新窗口显示PDF合同，请稍后",
            notice:"success"
        }, 3);
        rest.post({
            methodId: '5005',
            data: {contractId: contractId},
            success: function(res) {
                console.log(res);
                if (res.code == '0') {
                    window.open(res.res.data.filePath);
                } else {
                    tipService.show("body", {
                        message: res.res.msg,
                        notice: "danger"
                    }, 3);
                }
            }
        });

    }

    $scope.viewContractDialog = function(contractId){
        ngDialog.openConfirm({
          template: 'views/houseOwner/viewContract.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/controllers/houseOwner/viewContract.js'])
            }
          },
          data:{            
            contractId:contractId
          },
          scope: $scope,
          width: 1000,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });
    }
    //申请退租
    $scope.applyOffLease = function(contractId){
        ngDialog.openConfirm({
          template: 'js/module/fund/applyOffLease.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/module/fund/applyOffLease.js'])
            }
          },
          data:{       
            contractId: contractId,     
            status: "OrderPreOutMemberApply"
          },
          scope: $scope,
          width: 800,
          // height: 700
        }).then(function(data) {
            $scope.dataTable.fnClearTable();
        }, function(reason) {
            $scope.dataTable.fnClearTable();
        });
    }
    
    //同意退租
    $scope.agreeOffLease = function(contractId, exitDate){
        ngDialog.openConfirm({
          template: 'js/module/fund/agreeOffLease.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/module/fund/agreeOffLease.js'])
            }
          },
          data:{       
            contractId: contractId,     
            exitDate: exitDate,
            // status: "OrderPreOutApply"
          },
          scope: $scope,
          width: 800,
          // height: 700
        }).then(function(data) {
            $scope.dataTable.fnClearTable();
        }, function(reason) {
            $scope.dataTable.fnClearTable();
        });
    }

    //强制退租
    $scope.compelOffLease = function(contractId){
        ngDialog.openConfirm({
          template: 'js/module/fund/compelOffLease.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/module/fund/compelOffLease.js'])
            }
          },
          data:{       
            contractId: contractId,     
            status: "OrderOutOver"
          },
          scope: $scope,
          width: 800,
          // height: 700
        }).then(function(data) {
            $scope.dataTable.fnClearTable();
        }, function(reason) {
            $scope.dataTable.fnClearTable();
        });
    }
    $scope.qrcode = function (contractId, openId, title) {
        ngDialog.openConfirm({
          template: 'js/module/qrcode/qrcode2.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/module/qrcode/qrcode.js'])
            },
            deps: ['$ocLazyLoad',
              function($ocLazyLoad) {
                return $ocLazyLoad.load('monospaced.qrcode').then(function () {
                    return $ocLazyLoad.load(['js/module/qrcode/qrcode.js'])
                });
              }
            ]
          },
          data:{
            contractId: contractId,
            openId: openId,
            title: title
          },
          scope: $scope,
          width: 600,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });        
    }

    $scope.operation_none_mouseleave = function (e) {
        var obj = $(e.target).closest('.handle-td').find('.operation-none');
        obj.hide();
    }

    // 回调函数,返回实例化的jquery 插件对象
    $scope.initFinish = function(dataTable) {
        $scope.dataTable = dataTable;
    }
    $('#example thead').on('click', 'th', function() {
        // var data = $('#example').DataTable().row( this ).data();
        // alert('You clicked on  row');
    });
    $('#example tbody').on('click', 'span#edit', function() {
        var data = $('#example').DataTable().row($(this).parents('tr'));
        alert("查看修改：" + data[0]);
    });
    $('#example tbody').on('click', 'span#del', function() {
        // var data = $('#example').DataTable().row($(this).parents('tr')).data();
    });
});
